import React from 'react';
import { Button, Form, Input, message } from 'antd';
import '@/views/login/css/Index.css'
import { LoginAdmainHandler } from '@/api/login'
import { useAppDispatch } from '@/store/hooks';
import { Changeadminname,Changetoken,ChangecheckedKeys} from '@/store/modules/admin'
import { useNavigate } from 'react-router-dom';
const onFinishFailed = (errorInfo: any) => {
    console.log('Failed:', errorInfo);
};
const App: React.FC = () => {
    const [messageApi, contextHolder] = message.useMessage();
    const dispatch = useAppDispatch()
    const navigate =useNavigate()
    const onFinish = (values: {adminname:string,password:string}) => {
        LoginAdmainHandler(values).then(res => {
            console.log(res);
            if (res.data.data?.code === '10005') {
                messageApi.open({
                    type: 'error',
                    content: '该账号未注册！！',
                });
            } else if (res.data.data?.code === '10003') {
                messageApi.open({
                    type: 'warning',
                    content: '密码错误',
                });
            } else {
                messageApi.open({
                    type: 'success',
                    content: '登录成功啦^_^!!',
                });
                dispatch(Changeadminname(res.data.data?.adminname))
                dispatch(Changetoken(res.data.data?.token))
                dispatch(ChangecheckedKeys(res.data.data?.checkedkeys))            
                navigate('/')
            }
        })
    };
    return (
        <div className='Login-form-box'>
            {contextHolder}
            <div className='Login-form'>
                <h1>嗨购商城登录页面</h1>
                <Form
                    name="basic"
                    onFinish={onFinish}
                    onFinishFailed={onFinishFailed}
                    autoComplete="off"
                >
                    <Form.Item
                        name="adminname"
                        rules={[{ required: true, message: '请输入用户名！！！' }]}
                    >
                        <Input placeholder='管理员账号' />
                    </Form.Item>

                    <Form.Item
                        name="password"
                        rules={[{ required: true, message: '请输入密码！！！' }]}
                    >
                        <Input.Password placeholder='密码' />
                    </Form.Item>
                    <Form.Item >
                        <Button type="primary" htmlType="submit" block>
                            登录
                        </Button>
                    </Form.Item>
                </Form>
            </div>
        </div>
    )
}
export default App;